package com.ys.controller.api.outer;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.ys.config.MenuId;
import com.ys.config.MyConfig;
import com.ys.controller.api.business.ApiInterTpProductBqController;
import com.ys.entity.business.TpProductActive;
import com.ys.entity.business.TpProductActiveComponent;
import com.ys.entity.business.TpProductBasis;
import com.ys.entity.business.TpProductMaterial;
import com.ys.entity.business.TpProductActiveComponent;
import com.ys.entity.business.TpProductBasis;
import com.ys.entity.finance.TcwPartnerBankInfo;
import com.ys.entity.flow.YspModel;
import com.ys.entity.purchase.*;
import com.ys.entity.sys.TbCompanyBankInfo;
import com.ys.entity.sys.TsUserChange;
import com.ys.entity.sys.YsMenu;
import com.ys.entity.sys.YsProcInst;
import com.ys.enume.business.TpProductMaterialEnum;
import com.ys.enume.flow.FlowEnum;
import com.ys.enume.flow.YsProcInstEnum;
import com.ys.mapper.business.TpProductMaterialMapper;
import com.ys.enume.flow.FlowEnum;
import com.ys.enume.flow.YsProcInstEnum;
import com.ys.mapper.purchase.*;
import com.ys.service.business.TpProductBqService;
import com.ys.service.flow.YspTaskService;
import com.ys.service.purchase.TcgPurchaseContractService;
import com.ys.service.sys.CommonApiService;
import com.ys.util.DateUtil;
import com.ys.util.RedisUtil;
import com.ys.util.excel.ReadExcel;
import com.ys.util.shiro.ShiroUtils;
import com.ys.vo.Flow;
import com.ys.vo.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/api/outer/apiOuterDemoLzmExcelImport")
public class ApiOuterDemoLzmExcelImportController {

    private static final Logger logger = LoggerFactory.getLogger(ApiOuterDemoLzmExcelImportController.class);

	private static String URLAPI2_0 = MyConfig.getUrlApi1_5();


	@Autowired
	private CommonApiService<TcgPurchaseContract> commonApiService;


	@Autowired
	private TpProductBqService tpProductBqService;

	@Autowired
	private TpProductMaterialMapper tpProductMaterialMapper;



	/**
	 * 手动刷取采购合同数据
	 */
	@RequestMapping("/test1")
    @ResponseBody
	public R test1(HttpServletRequest request, HttpServletResponse response
			) {
		try {
			logger.info("*************开始*************");
			//读取本地文件
			FileInputStream fileInputStream = new FileInputStream(new File("D:/data/2024年销量前300的商品数据-168款(1)(1)1111.xlsx"));
			List<List<String>> res = ReadExcel.getRowsByInputStream(fileInputStream,0);
			System.out.println("res:\n"+res);

			//子表信息
			List<Map<String,Object>> dataList = new ArrayList<>();
			for(int i=1;i<res.size();i++){
				String vgsbm = "GS0904";//res.get(i).get(1);//公司编码
				String code = res.get(i).get(0);//商品编码
				if(code!=null && !"".equals(code)){
					Map<String,Object> mapNew = new HashMap<>();
					mapNew.put("vgsBm",vgsbm);
					mapNew.put("code",code);
					dataList.add(mapNew);
				}
			}

			String vgsbm = "GS0904";
			String commodityProperty = "优品";
			Map<String,Object> map = new HashMap<>();
			String bid = commonApiService.getCommonNextBid(ApiInterTpProductBqController.MENU_ID);
			map.put("commodityProperty",commodityProperty);
			map.put("bid",bid);

			map.put("vgsBm",vgsbm);

			List<Map<String,Object>> addlist = new ArrayList<>();

			for(Map<String,Object> item:dataList){
				//String vgsbm = item.get("vgsBm").toString();
				String code = item.get("code").toString();
				logger.info("****开始：vgsbm="+vgsbm+"；；；code = "+code);

				TpProductBasis tpProductBasis = new TpProductBasis()
						.addField("THRO.CUser AS cUser")
						.addLeftJoin("THR_OperateCompanyApply THRO ON  THRO.VGsBm = A.VgsBm")
						.setCode(code)
						.queryFirst();

				if(tpProductBasis!=null && tpProductBasis.getId()!=null){




					Map<String,Object> itemBySub = new HashMap<>();

					itemBySub.put("code","保存后自动生成");
					itemBySub.put("name","保存后自动生成");
					itemBySub.put("priceService","0");
					itemBySub.put("codeMerge",tpProductBasis.getCodeMerge());
					itemBySub.put("isSuperior","是");
					itemBySub.put("vsfJp",tpProductBasis.getVsfJp());
					itemBySub.put("spid",tpProductBasis.getId());
					itemBySub.put("commodityProperty","优品");
					addlist.add(itemBySub);


				}else {
					logger.info("！！！！！！！！！！未找到商品：vgsbm="+vgsbm+"；；；code = "+code);
				}


			}


			JSONArray array= JSONArray.parseArray(JSON.toJSONString(addlist));


			R resByAdd = tpProductBqService.add(map,array.toJSONString());
			logger.info("----------结果：vgsbm="+vgsbm+"；；；\nres="+resByAdd);


			logger.info("*************完成*************");
			return R.ok();
		}catch (Exception e){
			e.printStackTrace();
			logger.error(e.getMessage());
			//return R.error("操作失败!");
			throw new IllegalArgumentException("操作失败");
		}
	}






	/*
	* 刷取材质信息
	 */
	@RequestMapping("/test2")
	@ResponseBody
	public R test2(HttpServletRequest request, HttpServletResponse response
	) {
		try {
			logger.info("*************开始*************");

			List<TpProductActive> tpProductActiveList = new TpProductActive().setVgsBm("GS0904").queryList();
			List<TpProductBasis> tpProductBasisList = new TpProductBasis().setVgsBm("GS0904").queryList();

			for(TpProductActive tpProductActive:tpProductActiveList){
				int isAdd = new TpProductMaterial().setType(TpProductMaterialEnum.Type.TP_ProductActive.value).setProductCode(tpProductActive.getCode()).count();
				if(isAdd<=0){

					List<TpProductMaterial> tpProductMaterialList = new TpProductMaterial().setType(TpProductMaterialEnum.Type.TP_ProductActive.value).setProductCode(tpProductActive.getCodeMerge()).queryList();
					if(tpProductMaterialList!=null && tpProductMaterialList.size()>0){
						for (int j = 0; j < tpProductMaterialList.size(); j++) {
							Integer type = TpProductMaterialEnum.Type.TP_ProductActive.value;
							String productCode = tpProductActive.getCode();
							Long materialId = tpProductMaterialList.get(j).getMaterialId();
							String materialNameCn = tpProductMaterialList.get(j).getMaterialNameCn();
							String materialNameEn = tpProductMaterialList.get(j).getMaterialNameEn();
							TpProductMaterial tpProductMaterial = TpProductMaterial.tpProductMaterial(type, productCode, materialId, materialNameCn, materialNameEn, 1);
							tpProductMaterialMapper.insert(tpProductMaterial);
						}
					}
				}
			}

			for(TpProductBasis tpProductBasis:tpProductBasisList){
				int isAdd = new TpProductMaterial().setType(TpProductMaterialEnum.Type.TP_ProductBasis.value).setProductCode(tpProductBasis.getCode()).count();
				if(isAdd<=0){

					List<TpProductMaterial> tpProductMaterialList = new TpProductMaterial().setType(TpProductMaterialEnum.Type.TP_ProductBasis.value).setProductCode(tpProductBasis.getCodeMerge()).queryList();
					if(tpProductMaterialList!=null && tpProductMaterialList.size()>0){
						for (int j = 0; j < tpProductMaterialList.size(); j++) {
							Integer type = TpProductMaterialEnum.Type.TP_ProductActive.value;
							String productCode = tpProductBasis.getCode();
							Long materialId = tpProductMaterialList.get(j).getMaterialId();
							String materialNameCn = tpProductMaterialList.get(j).getMaterialNameCn();
							String materialNameEn = tpProductMaterialList.get(j).getMaterialNameEn();
							TpProductMaterial tpProductMaterial = TpProductMaterial.tpProductMaterial(type, productCode, materialId, materialNameCn, materialNameEn, 1);
							tpProductMaterialMapper.insert(tpProductMaterial);
							TpProductMaterial tpProductMaterial2 = TpProductMaterial.tpProductMaterial(TpProductMaterialEnum.Type.TP_ProductBqSp.value, productCode, materialId, materialNameCn, materialNameEn, 1);
							tpProductMaterialMapper.insert(tpProductMaterial2);
						}
					}
				}
			}


			logger.info("*************完成*************");
			return R.ok();
		}catch (Exception e){
			e.printStackTrace();
			logger.error(e.getMessage());
			//return R.error("操作失败!");
			throw new IllegalArgumentException("操作失败");
		}
	}






}
